package com.starbaba.mall.view;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.mall.service.FanliOrderService;
import com.starbaba.mall.service.impl.FanliOrderServiceImpl;
import com.starbaba.util.ExcelOperateUtil;
import com.tuanzi.server.loan_base.model.db.Loan_Mall_Fanli_Record;

/**
 * 返利订单 action
 * @author chenweijun 2017/10/29
 *
 */
@SuppressWarnings({"serial","unchecked","rawtypes"})
public class FanliOrderAction extends BaseAction implements ModelDriven<Loan_Mall_Fanli_Record>{
	
	private File taokeFile;                  //淘客订单列表文件
	private File ruyitouFile;                //如意投订单列表文件
	private String savePath;                 //导入文件路径
	private InputStream exportInputStream;   //导出文件输入流
	private String exportFileName;          //导出文件名

	Loan_Mall_Fanli_Record model;
	
	private FanliOrderService fanliOrderservice;
	
	public FanliOrderAction(){
		this.fanliOrderservice = new FanliOrderServiceImpl();
	}

	@Override
	public Loan_Mall_Fanli_Record getModel() {
		if(model == null){
			model = new Loan_Mall_Fanli_Record();
		}
		return model;
	}
	
	
	/**
	 * 查询订单列表
	 */
	public void getList(){
		
		parsePageRequest();
		PageResponse pageResponse=new PageResponse();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		List<Loan_Mall_Fanli_Record> list = fanliOrderservice.getList(paramsMap, pageRequest.getIDisplayStart(),pageRequest.getIDisplayLength());
		int count = fanliOrderservice.countList(paramsMap);
		try {
			pageResponse.setITotalDisplayRecords(count);
			pageResponse.setITotalRecords(count);
			pageResponse.setAaData(list);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch(IOException e) {
			LogService.error(e);
		}
		
	}
	

	/**
	 * 导入淘客订单列表
	 */
	public void importTaokeOrderList(){
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		File file = taokeFile;
		
		if (file.exists()) {
			String fileName = file.getAbsolutePath();
			fileName = fileName.replace(".tmp", ".xls");
			file.renameTo(new File(fileName));
			file = new File(fileName);	
		}
		
		//处理excel文件
		ExcelOperateUtil poi = new ExcelOperateUtil();
		String[][] result = poi.getData(file);
		
		int num = 0;
		try {
			
			 num = fanliOrderservice.importTaokeOrderList(result);
			 JsonObject resultJson = new JsonObject();
			 resultJson.addProperty("total", result.length);
			 resultJson.addProperty("num", num);
			 sendJsonMessage(resultJson.toString());
			 
		} catch (AppException e) {
			LogService.error(e);
		} catch (IOException e) {
			LogService.error(e);
		}
		
		
	}
	
	/**
	 * 导入如意投订单列表
	 */
	public void importRuyitouOrderList(){
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		File file = ruyitouFile;
		
		if (file.exists()) {
			String fileName = file.getAbsolutePath();
			fileName = fileName.replace(".tmp", ".xls");
			file.renameTo(new File(fileName));
			file = new File(fileName);	
		}
		
		//处理excel文件
		ExcelOperateUtil poi = new ExcelOperateUtil();
		String[][] result = poi.getData(file);
		
		int num = 0;
		try {
			
			 num = fanliOrderservice.importRuyitouOrderList(result);
			 JsonObject resultJson = new JsonObject();
			 resultJson.addProperty("total", result.length);
			 resultJson.addProperty("num", num);
			 sendJsonMessage(resultJson.toString());
			 
		} catch (AppException e) {
			LogService.error(e);
		} catch (IOException e) {
			LogService.error(e);
		}
		
		
	}
	
	/**
	 * 导出列表
	 */
	public String exportList(){
		
		parsePageRequest();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		String result = fanliOrderservice.exportList(paramsMap);
		  try {
			  
			  exportInputStream = new ByteArrayInputStream(result.getBytes("GBK"));
			  exportFileName = "fanli_order" + System.currentTimeMillis() + ".csv";
			  
		} catch (UnsupportedEncodingException e) {
			LogService.error(e);
			return ERROR;
		}
		return SUCCESS;
		
	}
	

	public File getTaokeFile() {
		return taokeFile;
	}

	public void setTaokeFile(File taokeFile) {
		this.taokeFile = taokeFile;
	}
	
	public File getRuyitouFile() {
		return ruyitouFile;
	}

	public void setRuyitouFile(File ruyitouFile) {
		this.ruyitouFile = ruyitouFile;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public InputStream getExportInputStream() {
		return exportInputStream;
	}

	public void setExportInputStream(InputStream exportInputStream) {
		this.exportInputStream = exportInputStream;
	}

	public String getExportFileName() {
		return exportFileName;
	}

	public void setExportFileName(String exportFileName) {
		this.exportFileName = exportFileName;
	}


}
